class ChartController < ApplicationController
  def shows(params,s)
    @shows = "?"
    if params[:sF] && s != "sF"
      @shows = @shows + "&amp;" unless @shows == "?"
      @shows = @shows + "sF=f"
    end
    if params[:sS] && s != "sS"
      @shows = @shows + "&amp;" unless @shows == "?"
      @shows = @shows + "sS=f"
    end
    if params[:sT] && s != "sT"
      @shows = @shows + "&amp;" unless @shows == "?"
      @shows = @shows + "sT=f"
    end
    if params[:sY] && s != "sY"
      @shows = @shows + "&amp;" unless @shows == "?"
      @shows = @shows + "sY=f"
    end
    if params[:type] && s != "type"
      @shows = @shows + "&amp;" unless @shows == "?"
      @shows = @shows + "type=m"
    end
    if params[:sumType] && s != "sumType"
      @shows = @shows + "&amp;" unless @shows == "?"
      @shows = @shows + "sumType=a"
    end
    return @shows
  end

  def add_hide_link(chart,params,s,x,url)
    @dates = ""
    @tt = "Hide Flickr" unless s!="sF"
    @tt = "Hide Slideshare" unless s!="sS"
    @tt = "Hide Twitter" unless s!="sT"
    @tt = "Hide Youtube" unless s!="sY"
    if params[:m] && params[:y]
      @dates = "&amp;m=#{params[:m]}&amp;y=#{params[:y]}"
    end
    if params[:type] && params[:sumType]
      chart.add_link_area(x,'60','150','30','update','1',"#{url}#{shows(params,s)}&amp;#{s}=f#{@dates}",@tt) unless shows(params,s).length > 47
    end
    if !params[:type] && params[:sumType]
      chart.add_link_area(x,'60','150','30','update','1',"#{url}#{shows(params,s)}&amp;#{s}=f#{@dates}",@tt) unless shows(params,s).length > 35
    end
    if params[:type] && !params[:sumType]
      chart.add_link_area(x,'60','150','30','update','1',"#{url}#{shows(params,s)}&amp;#{s}=f#{@dates}",@tt) unless shows(params,s).length > 33
    end
    if !params[:type] && !params[:sumType]
      chart.add_link_area(x,'60','150','30','update','1',"#{url}#{shows(params,s)}&amp;#{s}=f#{@dates}",@tt) unless shows(params,s).length > 22
    end
    return chart
  end
  def add_show_link(chart,params,s,x,url)
    @dates = ""
    @tt = "Show Flickr" unless s!="sF"
    @tt = "Show Slideshare" unless s!="sS"
    @tt = "Show Twitter" unless s!="sT"
    @tt = "Show Youtube" unless s!="sY"

    if params[:m] && params[:y]
      @dates = "&amp;" unless shows(params,s) == "?"
      @dates = "&amp;m=#{params[:m]}&amp;y=#{params[:y]}"
    end
    chart.add_link_area(x,'60','150','30','update','1',"#{url}#{shows(params,s)}#{@dates}",@tt)
    chart.add_draw_area("rect",x,'65','150','25','ffffff','75')
    return chart
  end

  def load_line
    @line_chart_url = "#{ActionController::Base.relative_url_root}/chart/load_line/#{params[:id]}"
    @col_chart_url = "#{ActionController::Base.relative_url_root}/chart/load_col/#{params[:id]}"
    months = Date::ABBR_MONTHNAMES
    chart = Chart.new(4)
    chart.chart_type='line'
    chart.axis_category={'size'=>'12','orientation'=>'horizontal'}
    chart.axis_value={'size'=>'12'}
    chart.legend={'size'=>'14','y'=>'60'}
    chart.chart_note={'type'=>'flag','size'=>'10','background_color'=>'880000'}
    chart.add_category('')
    chart.add_series_value(0,"Flickr",nil)
    chart.add_series_value(1,"Slideshare",nil)
    chart.add_series_value(2,"Twitter",nil)
    chart.add_series_value(3,"Youtube",nil)


    @time = Time.new
    if params[:y]
      @year = params[:y].to_i
    else
      @year = @time.year.to_i
      if !params[:type]
        @year = @year -1
      end
    end
    if params[:m]
      @month = params[:m].to_i
    else
      @month = @time.month.to_i + 1
      if !params[:type]
        @year = @year +1 unless @month != 13
        @month = 1 unless @month != 13
      end
    end    

    @back_month = @month - 1
    @back_month = 12 unless @back_month != 0
    @back_year = @year
    @back_year = @back_year - 1 unless @back_month != 12
    @next_month = @month + 1
    @next_month = 1 unless @next_month != 13
    @next_year = @year
    @next_year = @next_year + 1 unless @next_month != 1
    chart.add_link_area('80','308','20','12','update','1',"#{@line_chart_url}#{shows(params,"s")}&amp;m=#{@back_month}&amp;y=#{@back_year}","Go Back One Month")
    chart.add_draw_area("text",'80','304','25','12','000000','<<')
    chart.add_link_area('730','308','20','12','update','1',"#{@line_chart_url}#{shows(params,"s")}&amp;m=#{@next_month}&amp;y=#{@next_year}","Go Forward One Month")
    chart.add_draw_area("text",'730','304','25','12','000000','>>')

    @title1 = Tag.find(:first, :conditions => {:id => params[:id]}).name.upcase
    @title2 = Tag.find(:first, :conditions => {:id => params[:id]}).description
    if params[:sumType]
      @title3 = "accumulated - showing "       
      if !params[:type]
        @title3 = @title3 + "#{months[@month]}/#{@year} to #{months[(@month-1)]}/#{(@year+1)}" unless @month == 1
        @title3 = @title3 + "Jan/#{@year} to Dec/#{(@year)}" unless @month != 1
      else
        @title3 = @title3 + "1/#{months[@month]}/#{@year} to #{Date.civil(@year,@month,-1).day().to_i}/#{months[@month]}/#{@year}"
      end
    else 
      @title3 = "showing "       
      if !params[:type]
        @title3 = @title3 + "#{months[@month]}/#{@year} to #{months[(@month-1)]}/#{(@year+1)}" unless @month == 1
        @title3 = @title3 + "Jan/#{@year} to Dec/#{(@year)}" unless @month != 1
      else
        @title3 = @title3 + "1/#{months[@month]}/#{@year} to #{Date.civil(@year,@month,-1).day().to_i}/#{months[@month]}/#{@year}"
      end
    end
    chart.add_draw_area("text",'0','0','800','20','000000',"#{@title1}")
    chart.add_draw_area("text",'0','30','800','12','000000',"#{@title2}")
    chart.add_draw_area("text",'0','330','800','12','000000',"#{@title3}")

    chart.add_link_area('0','75','100','12','update','1',"#{@col_chart_url}#{shows(params,"type")}","Show the Totals as a bar chart")
    chart.add_draw_area("text",'0','75','100','12','0000FF','Show Totals')

    if params[:sumType]
      chart.add_link_area('0','100','100','28','update','1',"#{@line_chart_url}#{shows(params,"sumType")}&amp;m=#{@month}&amp;y=#{@year}","Change graph type to incremental (non-accumulated)")
      chart.add_draw_area("text",'0','100','100','12','0000FF','Change To \rAccumulated')
    else
      chart.add_link_area('0','100','100','28','update','1',"#{@line_chart_url}#{shows(params,"s")}&amp;m=#{@month}&amp;y=#{@year}&amp;sumType=t","Change graph type to accumulated")
      chart.add_draw_area("text",'0','100','100','12','0000FF','Change To \rIncremental')
    end

    if !params[:type]
      chart.add_draw_area("text",'0','135','100','12','0000FF',"Export CSV") 
      chart.add_link_area('0','135','100','20',"_Blank",'1',"#{ActionController::Base.relative_url_root}/data/year_csv/#{params[:id]}?site=all&amp;y=#{@year}&amp;m=#{@month}","Export data on graph to CSV")
    else
      chart.add_draw_area("text",'0','150','100','12','0000FF',"Year View") 
      chart.add_link_area('0','150','100','20',"update",'1',"#{@line_chart_url}#{shows(params,"type")}","Go back to year view")
      chart.add_draw_area("text",'0','135','100','12','0000FF',"Export CSV") 
      chart.add_link_area('0','135','100','20',"_Blank",'1',"#{ActionController::Base.relative_url_root}/data/month_csv/#{params[:id]}?site=all&amp;y=#{@year}&amp;m=#{@month}","Export data on graph to CSV")
    end

    chart = add_hide_link(chart,params,"sF","120",@line_chart_url) unless params[:sF]
    chart = add_show_link(chart,params,"sF","120",@line_chart_url) unless !params[:sF]
    chart = add_hide_link(chart,params,"sS","275",@line_chart_url) unless params[:sS]
    chart = add_show_link(chart,params,"sS","275",@line_chart_url) unless !params[:sS]
    chart = add_hide_link(chart,params,"sT","430",@line_chart_url) unless params[:sT]
    chart = add_show_link(chart,params,"sT","430",@line_chart_url) unless !params[:sT]
    chart = add_hide_link(chart,params,"sY","585",@line_chart_url) unless params[:sY]
    chart = add_show_link(chart,params,"sY","585",@line_chart_url) unless !params[:sY]

    chart.link_data={'url'=>"#{ActionController::Base.relative_url_root}/data/#{params[:id]}?year=#{@year}&amp;month=#{@month}",'target'=>'_blank', 'category'=>'true'}

    @day = 1
    @counter = 0
    while @counter < 12 && @day <= Date.civil(@year,@month,-1).day().to_i
      if params[:sumType]
        @sql_m = "WHERE tag_id=#{params[:id]} AND ((upload_year<#{@year}) OR (upload_year=#{@year} AND upload_month<#{@month}) OR  (upload_year=#{@year} AND upload_month=#{@month} AND upload_day<=#{@day}))"
        @sql_y = "WHERE tag_id=#{params[:id]} AND ((upload_year<#{@year}) OR (upload_year=#{@year} AND upload_month<=#{@month}))"
      else
        @sql_m = "WHERE tag_id=#{params[:id]} AND upload_year=#{@year} AND upload_month=#{@month} AND upload_day=#{@day}"
        @sql_y = "WHERE tag_id=#{params[:id]} AND upload_year=#{@year} AND upload_month=#{@month}"
      end 

      if !params[:type]
        chart.add_category("#{months[@month]}")
        chart.add_link_area((138+(50*(@counter.to_i))).to_s,'305','25','25','update','1',"#{@line_chart_url}#{shows(params,"s")}&amp;type=m&amp;m=#{@month}&amp;y=#{@year}","View data for #{months[@month]}")
      else
        chart.add_category("#{@day}")
      end    

      if !params[:sF]
        if !params[:type]
          flickr_data_points = Flickr.find_by_sql("SELECT count(id) AS count FROM flickrs #{@sql_y} LIMIT 1;")
        else
          flickr_data_points = Flickr.find_by_sql("SELECT count(id) AS count FROM flickrs #{@sql_m} LIMIT 1;")
        end
        if flickr_data_points.length > 0
          decorations = Hash.new()
          decorations['tooltip'] = "Flickr: #{flickr_data_points.first.count}"
          chart.add_series_value(0,"#{flickr_data_points.first.count}",decorations)
        else
          decorations = Hash.new()
          decorations['tooltip'] = "Flickr: 0"
          chart.add_series_value(0,"0",decorations)
        end
      end

      if !params[:sS]
        if !params[:type]
          slideshare_data_points = Slideshare.find_by_sql("SELECT count(id)AS count FROM slideshares #{@sql_y} LIMIT 1;")
        else
          slideshare_data_points = Slideshare.find_by_sql("SELECT count(id)AS count FROM slideshares #{@sql_m} LIMIT 1;")
        end
        if slideshare_data_points.length > 0
          decorations = Hash.new()
          decorations['tooltip'] = "Slideshare: #{slideshare_data_points.first.count}"
          chart.add_series_value(1,"#{slideshare_data_points.first.count}",decorations)
        else
          decorations = Hash.new()
          decorations['tooltip'] = "Slideshare: 0"
          chart.add_series_value(1,"0",decorations)
        end
      end

      if !params[:sT]
        if !params[:type]
          tweet_data_points = Tweet.find_by_sql("SELECT count(id)AS count FROM tweets #{@sql_y} LIMIT 1;")
        else
          tweet_data_points = Tweet.find_by_sql("SELECT count(id)AS count FROM tweets #{@sql_m} LIMIT 1;")
        end
        if tweet_data_points.length > 0
          decorations = Hash.new()
          decorations['tooltip'] = "Tweet: #{tweet_data_points.first.count}"
          chart.add_series_value(2,"#{tweet_data_points.first.count}",decorations)
        else
          decorations = Hash.new()
          decorations['tooltip'] = "Tweet: 0"
          chart.add_series_value(2,"0",decorations)
        end
      end

      if !params[:sY]
        if !params[:type]
          youtube_data_points = Youtube.find_by_sql("SELECT count(id)AS count FROM youtubes #{@sql_y} LIMIT 1;")
        else
          youtube_data_points = Youtube.find_by_sql("SELECT count(id)AS count FROM youtubes #{@sql_m} LIMIT 1;")
        end
        if youtube_data_points.length > 0
          decorations = Hash.new()
          decorations['tooltip'] = "Youtube: #{youtube_data_points.first.count}"
          chart.add_series_value(3,"#{youtube_data_points.first.count}",decorations)
        else
          decorations = Hash.new()
          decorations['tooltip'] = "Youtube: 0"
          chart.add_series_value(3,"0",decorations)
        end
      end
      if !params[:type]
        if @month == 12
          @month = 1
          @year = @year + 1
        else
          @month = @month + 1
        end
        @counter = @counter + 1
      else
        @day = @day + 1
      end
    end   
    
    respond_to do |fmt|
      fmt.xml {render :xml => chart.to_xml}
    end
  end

  def load_col
    @col_chart_url = "#{ActionController::Base.relative_url_root}/chart/load_col/#{params[:id]}"
    @line_chart_url = "#{ActionController::Base.relative_url_root}/chart/load_line/#{params[:id]}"

    months = Date::ABBR_MONTHNAMES
    chart = Chart.new(4)
    chart.chart_type='column'
    chart.axis_category={'size'=>'12','orientation'=>'horizontal'}
    chart.axis_value={'size'=>'12'}
    chart.legend={'size'=>'14','y'=>'60'}
    chart.chart_note={'type'=>'flag','size'=>'10','background_color'=>'880000'}
    chart.add_category('')
    chart.add_category("Total")
    chart.add_series_value(0,"Flickr",nil)
    chart.add_series_value(1,"Slideshare",nil)
    chart.add_series_value(2,"Twitter",nil)
    chart.add_series_value(3,"Youtube",nil)

    chart.add_draw_area("text",'0','0','800','20','000000',"#{Tag.find(:first, :conditions => {:id => params[:id]}).name}".upcase)
    chart.add_draw_area("text",'0','30','800','12','000000',"#{Tag.find(:first, :conditions => {:id => params[:id]}).description}")
    chart.add_draw_area("text",'0','330','800','12','000000',"Showing the totals for all time")

    chart.add_link_area('0','75','100','12','update','1',"#{@line_chart_url}#{shows(params,"type")}","Show the Detailed Line Chart")
    chart.add_draw_area("text",'0','75','100','12','0000FF','Show Details')

    chart = add_hide_link(chart,params,"sF","120",@col_chart_url) unless params[:sF]
    chart = add_show_link(chart,params,"sF","120",@col_chart_url) unless !params[:sF]
    chart = add_hide_link(chart,params,"sS","275",@col_chart_url) unless params[:sS]
    chart = add_show_link(chart,params,"sS","275",@col_chart_url) unless !params[:sS]
    chart = add_hide_link(chart,params,"sT","430",@col_chart_url) unless params[:sT]
    chart = add_show_link(chart,params,"sT","430",@col_chart_url) unless !params[:sT]
    chart = add_hide_link(chart,params,"sY","585",@col_chart_url) unless params[:sY]
    chart = add_show_link(chart,params,"sY","585",@col_chart_url) unless !params[:sY]

    chart.link_data={'url'=>"#{ActionController::Base.relative_url_root}/data/#{params[:id]}?",'target'=>'_blank', 'category'=>'true'}
    
    if !params[:sF]  
      flickr_data_points = Flickr.find_by_sql("SELECT count(id) AS count FROM flickrs WHERE tag_id=#{params[:id]} LIMIT 1;")
      if flickr_data_points.length > 0
        decorations = Hash.new()
        decorations['tooltip'] = "Flickr: #{flickr_data_points.first.count}"
        chart.add_series_value(0,"#{flickr_data_points.first.count}",decorations)
      else
        decorations = Hash.new()
        decorations['tooltip'] = "Flickr: 0"
        chart.add_series_value(0,"0",decorations)
      end
    end

    if !params[:sS]
      slideshare_data_points = Slideshare.find_by_sql("SELECT count(id)AS count FROM slideshares WHERE tag_id=#{params[:id]} LIMIT 1;")
      if slideshare_data_points.length > 0
        decorations = Hash.new()
        decorations['tooltip'] = "Slideshare: #{slideshare_data_points.first.count}"
        chart.add_series_value(1,"#{slideshare_data_points.first.count}",decorations)
      else
        decorations = Hash.new()
        decorations['tooltip'] = "Slideshare: 0"
        chart.add_series_value(1,"0",decorations)
      end
    end

    if !params[:sT]
      tweet_data_points = Tweet.find_by_sql("SELECT count(id)AS count FROM tweets WHERE tag_id=#{params[:id]} LIMIT 1;")
      if tweet_data_points.length > 0
        decorations = Hash.new()
        decorations['tooltip'] = "Tweet: #{tweet_data_points.first.count}"
        chart.add_series_value(2,"#{tweet_data_points.first.count}",decorations)
      else
        decorations = Hash.new()
        decorations['tooltip'] = "Tweet: 0"
        chart.add_series_value(2,"0",decorations)
      end
    end

    if !params[:sY]
      youtube_data_points = Youtube.find_by_sql("SELECT count(id)AS count FROM youtubes WHERE tag_id=#{params[:id]} LIMIT 1;")
      if youtube_data_points.length > 0
        decorations = Hash.new()
        decorations['tooltip'] = "Youtube: #{youtube_data_points.first.count}"
        chart.add_series_value(3,"#{youtube_data_points.first.count}",decorations)
      else
        decorations = Hash.new()
        decorations['tooltip'] = "Youtube: 0"
        chart.add_series_value(3,"0",decorations)
      end
    end
    respond_to do |fmt|
      fmt.xml {render :xml => chart.to_xml}
    end
  end
end
